package jzqb.service.impl;

import haha.life.common.CutPageBean;

import jzqb.bo.AdminPermissionGroupBo;
import jzqb.bo.AdminPermissionGroup_getuserBo;
import jzqb.bo.QBZSBo;
import jzqb.bo.StandardBo;
import jzqb.common.Result;
import jzqb.dao.AdminDepartmentDao;
import jzqb.dao.AdminPermissionDao;

import jzqb.dao.AdminUserInfoDao;
import jzqb.po.AdminDepartmentPo;
import jzqb.po.AdminPermissionGroupPo;
import jzqb.po.AdminUserPo;
import jzqb.service.AdminPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminPermissionServiceImpl implements AdminPermissionService {

    @Autowired
    private AdminPermissionDao adminPermissionDao;
    @Autowired
    private AdminUserInfoDao adminUserInfoDao;
    @Autowired
    private AdminDepartmentDao adminDepartmentDao;



    @Override
    public CutPageBean<AdminPermissionGroupPo> cutPage (AdminPermissionGroupBo adminPermissionGroupBo){
        CutPageBean<AdminPermissionGroupPo> cutPageBean = new CutPageBean<>();
        List<AdminPermissionGroupPo> adminPermissionGroupList  = adminPermissionDao.cutList(adminPermissionGroupBo);
        //用户成员显示真实名字
        List<AdminUserPo> allUser = adminUserInfoDao.findAllUser();
        for (int i = 0; i < adminPermissionGroupList.size(); i++) {
            AdminPermissionGroupPo adminPermissionGroupPo = adminPermissionGroupList.get(i);
            List<String> showNameList = new ArrayList<String>();
            for (int j = 0; j < adminPermissionGroupPo.getUsers().split(";").length; j++) {
                String singleUser = adminPermissionGroupPo.getUsers().split(";")[j];
                for (int k = 0; k < allUser.size(); k++) {
                    AdminUserPo adminUserPo = allUser.get(k);
                    if (singleUser.equals(adminUserPo.getId())){
                        showNameList.add(adminUserPo.getName());
                        break;
                    }
                }
            }
            adminPermissionGroupPo.setShowUsers(String.join(";",showNameList));
        }
        //部门成员显示真实名字
        List<AdminDepartmentPo> allDepartment = adminDepartmentDao.findAllDept();
        for (int i = 0; i < adminPermissionGroupList.size(); i++) {
            AdminPermissionGroupPo adminPermissionGroupPo = adminPermissionGroupList.get(i);
            List<String> showDeptList = new ArrayList<String>();
            for (int j = 0; j < adminPermissionGroupPo.getDepartments().split(";").length; j++) {
                String singleDept = adminPermissionGroupPo.getDepartments().split(";")[j];
                for (int k = 0; k < allDepartment.size(); k++) {
                    AdminDepartmentPo adminDepartmentPo = allDepartment.get(k);
                    if (singleDept.equals(adminDepartmentPo.getDepartmentCode())){
                        showDeptList.add(adminDepartmentPo.getDepartmentName());
                        break;
                    }
                }
            }
            adminPermissionGroupPo.setShowDepartments(String.join(";",showDeptList));
        }

        int count=(int) adminPermissionDao.cutCount(adminPermissionGroupBo);
        cutPageBean.initCutPage(count, adminPermissionGroupBo.getPageSize(),adminPermissionGroupList);
        return cutPageBean;
    }

    /*
    * 选择用户,用户名和id合并检索
    * */
    @Override
    public CutPageBean<AdminUserPo> cutPage_ (AdminPermissionGroup_getuserBo adminUserInfoBo){
        CutPageBean<AdminUserPo> cutPageBean = new CutPageBean<>();
        List<AdminUserPo> adminuserList  = adminPermissionDao.cutList_(adminUserInfoBo);

        //部门成员显示真实名字
        List<AdminDepartmentPo> allDepartment = adminDepartmentDao.findAllDept();
        for (AdminUserPo adminUserPo:adminuserList) {
            List<String> showDepList = new ArrayList<String>();
            for (String depID:adminUserPo.getDepartmentid().split(";")) {
                for (AdminDepartmentPo adminDepartmentPo:allDepartment) {
                    if (adminDepartmentPo.getDepartmentCode().equals(depID)){
                        showDepList.add(adminDepartmentPo.getDepartmentName());
                        break;
                    }
                }
            }
            adminUserPo.setShowDepartment(String.join(";",showDepList));
        }
        int count=(int) adminPermissionDao.cutCount_(adminUserInfoBo);
        cutPageBean.initCutPage(count, adminUserInfoBo.getPageSize(),adminuserList);
        return cutPageBean;
    }

    /*
     * 展示用户,switch按钮
     * */
    @Override
    public CutPageBean<AdminUserPo> cutPage_switchShow (AdminPermissionGroup_getuserBo adminUserInfoBo){

        CutPageBean<AdminUserPo> cutPageBean = new CutPageBean<>();

        //特殊情况:没有 用户成员 的情况
        if (("").equals(adminUserInfoBo.getIds())){
            cutPageBean.initCutPage(0, adminUserInfoBo.getPageSize(),null);
            return cutPageBean;
        }

        List<AdminUserPo> adminuserList  = adminPermissionDao.cutList_switchShow(adminUserInfoBo);

        //部门成员显示真实名字
        List<AdminDepartmentPo> allDepartment = adminDepartmentDao.findAllDept();
        for (AdminUserPo adminUserPo:adminuserList) {
            List<String> showDepList = new ArrayList<String>();
            for (String depID:adminUserPo.getDepartmentid().split(";")) {
                for (AdminDepartmentPo adminDepartmentPo:allDepartment) {
                    if (adminDepartmentPo.getDepartmentCode().equals(depID)){
                        showDepList.add(adminDepartmentPo.getDepartmentName());
                        break;
                    }
                }
            }
            adminUserPo.setShowDepartment(String.join(";",showDepList));
        }
        int count=(int) adminPermissionDao.cutCount_switchShow(adminUserInfoBo);
        cutPageBean.initCutPage(count, adminUserInfoBo.getPageSize(),adminuserList);
        return cutPageBean;
    }

    @Override
    public Boolean addeditPermissionGroup(AdminPermissionGroupPo adminPermissionGroupPo){
        return  adminPermissionDao.addeditPermissionGroup(adminPermissionGroupPo);
    };

    @Override
    public Boolean deletePermissionGroup(AdminPermissionGroupPo adminPermissionGroupPo){
        return  adminPermissionDao.deletePermissionGroup(adminPermissionGroupPo);
    };

    @Override
    public Result getPermissionTreeExisted(String permissionGroupID){
        return  adminPermissionDao.getPermissionTreeExisted(permissionGroupID);
    };

    @Override
    public boolean checkDataPermission(String userid, QBZSBo qbzsBo, String accessType){
        return  adminPermissionDao.checkDataPermission(userid,qbzsBo,accessType);
    };

    @Override
    public boolean addBrowseDownLoadLog(String operateType, String userId, StandardBo bo) {
        return  adminPermissionDao.addBrowseDownLoadLog(operateType,userId,bo);
    }
}
